package com.kern.socketdemo.demo1;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 *  TCP通信, 客户端
 * Created by caoshi at 7:29 2021-07-22
 */
public class TCPClient {

    public static void main(String[] args) throws IOException {
        // 客户端socket, 连接服务端的 ip 和 port
        Socket socket = new Socket("127.0.0.1", 8989);

        // 获取输出流, 准备向服务端发送数据
        OutputStream out = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(out);

        String dataStr = "服务端你好!我是TCP客户端";
        // 写入数据
        dos.writeUTF(dataStr);

        // 关闭io流和socket
        dos.close();
        out.close();
        socket.close();
    }


}
